import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:toast/toast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class RegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: RegisterPage2(title: "LoginPage"),
    );
  }
}

class RegisterPage2 extends StatefulWidget {
  RegisterPage2({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _RegisterPage2State createState() => _RegisterPage2State();
}

class _RegisterPage2State extends State<RegisterPage2>{
  bool _passwordVisibleFlag = true;
  bool _passwordVisibleFlag2 = true;

  int _errorFlag = 0;

  TextEditingController _editingController0=new TextEditingController();//用户名栏的控制器
  TextEditingController _editingController1=new TextEditingController();//密码栏的控制器
  TextEditingController _editingController2=new TextEditingController();//确认密码栏的控制器
  String userName,passWord,ensurePassWord;

  void _getText(){
    setState(() {
      userName=_editingController0.text;
      passWord=_editingController1.text;
      ensurePassWord=_editingController2.text;
      print("userName: "+userName+"\npassWord: "+passWord+"\nensurePassWord: "+ensurePassWord);
      if(userName.length>=10 && userName.length<=20){

      }else{
        _errorFlag=1;//1表示用户名不合法
        Toast.show("用户名一栏不合法", context);
      }
      if(passWord == ensurePassWord){
        if(passWord.length>=8 && passWord.length<=15){

        }else{
          _errorFlag=3;//3表示密码不合法
          Toast.show("密码一栏不合法", context);
        }
      }else{
        _errorFlag=2;//2表示两次密码不一致
        Toast.show("两次密码输入不一致", context);
      }
    });
  }

  Widget build(BuildContext context) {
    save() async{
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString(userName, passWord);
    }
    // Future<String> get(String mUserName) async{
    //   var mPassWord;
    //   SharedPreferences prefs = await SharedPreferences.getInstance();
    //   mPassWord = prefs.getString(mUserName);
    //   return mPassWord;
    // }

    return Scaffold(
      appBar: AppBar(
        title: Text("RegisterPage", style: TextStyle(),),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Align(
              alignment: Alignment.topLeft,
              child: Column(
                children: <Widget>[
                  Align(
                      alignment: Alignment.topLeft,
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(20, 20,0,0),
                        child: Image.asset(
                          "images/baidu_logo.png",
                          width: 120,
                        ),
                      )
                  ),
                  Align(
                      alignment: Alignment.topLeft,
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(20, 0,0,0),
                        child: Text(
                          "欢迎注册百度帐户",
                          textAlign: TextAlign.left,
                          style: TextStyle(
                              fontSize: 30,
                              fontWeight: FontWeight.bold
                          ),
                        ),
                      )
                  ),
                ],
              ),
            ),
            SizedBox(height: 100,),
            Padding(
                padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                child: Container(
                  height: 60,
                  child: TextField(
                    controller: _editingController0,
                    keyboardType: TextInputType.url,
                    maxLines: 1,//最大行数
                    inputFormatters: [WhitelistingTextInputFormatter(RegExp(
                        "[a-zA-Z]|[0-9]")),//限制输入内容为英语、数字
                      LengthLimitingTextInputFormatter(15)//限制字符长度
                    ],
                    decoration: InputDecoration(
                      // labelText: "账户名/手机号/邮箱",
                      // labelStyle: TextStyle(
                      //   color: Colors.grey,
                      //   fontSize: 16
                      // ),
                      // hintText: "输入...",
                        hintText: "请输入手机号/用户名/邮箱（数字或字母，区分大小写）",
                        hintStyle: TextStyle(fontSize: 16),
                        fillColor: Colors.black12,//背景颜色，必须配合filled为true才能显示
                        filled: true,//背景颜色，必须配合fillColor才能显示
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                            borderSide: BorderSide.none
                        )
                    ),
                  ),
                )
            ),
            Padding(
                padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
                child: Container(
                  height: 60,
                  child: TextField(
                    controller: _editingController1,
                    keyboardType: TextInputType.url,
                    maxLines: 1,//最大行数
                    obscureText: _passwordVisibleFlag,
                    inputFormatters: [WhitelistingTextInputFormatter(RegExp(
                      "[a-zA-Z]|[0-9]")),//限制输入内容为英语、数字
                    LengthLimitingTextInputFormatter(15)//限制字符长度
                    ],
                    decoration: InputDecoration(
                      // labelText: "密码",
                      // labelStyle: TextStyle(
                      //     color: Colors.grey,
                      //     fontSize: 16
                      // ),
                      // hintText: "输入...",
                        hintText: "请输入8~15位的数字或字母密码（区分大小写）",
                        hintStyle: TextStyle(fontSize: 16),
                        fillColor: Colors.black12,//背景颜色，必须配合filled为true才能显示
                        filled: true,//背景颜色，必须配合fillColor才能显示
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                            borderSide: BorderSide.none
                        ),
                        suffixIcon: IconButton(
                          icon: Icon(_passwordVisibleFlag ? Icons.visibility : Icons.visibility_off),
                          onPressed: (){
                            setState(() {
                              _passwordVisibleFlag = !_passwordVisibleFlag;
                            });
                          },
                        )
                    ),
                  ),
                )
            ),
            Padding(
                padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
                child: Container(
                  height: 60,
                  child: TextField(
                    controller: _editingController2,
                    keyboardType: TextInputType.url,
                    maxLines: 1,//最大行数
                    obscureText: _passwordVisibleFlag2,
                    inputFormatters: [WhitelistingTextInputFormatter(RegExp(
                        "[a-zA-Z]|[0-9]")),//限制输入内容为英语、数字
                      LengthLimitingTextInputFormatter(15)//限制字符长度
                    ],
                    decoration: InputDecoration(
                      // labelText: "密码",
                      // labelStyle: TextStyle(
                      //     color: Colors.grey,
                      //     fontSize: 16
                      // ),
                      // hintText: "输入...",
                        hintText: "请再次确认登陆密码",
                        hintStyle: TextStyle(fontSize: 16),
                        fillColor: Colors.black12,//背景颜色，必须配合filled为true才能显示
                        filled: true,//背景颜色，必须配合fillColor才能显示
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                            borderSide: BorderSide.none
                        ),
                        suffixIcon: IconButton(
                          icon: Icon(_passwordVisibleFlag2 ? Icons.visibility : Icons.visibility_off),
                          onPressed: (){
                            setState(() {
                              _passwordVisibleFlag2 = !_passwordVisibleFlag2;
                            });
                          },
                        )
                    ),
                  ),
                )
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
              child: RaisedButton(
                color: Colors.blue,
                child: Container(
                  alignment: Alignment.center,
                  child: Text('注  册',
                    style: TextStyle(fontSize: 20,color: Colors.white),
                  ),
                  height: 60,
                ),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                onPressed: (){
                  _getText();
                  if(_errorFlag==0){
                    Toast.show('注册成功', context);
                    save();
                    Navigator.of(context).pop();
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}